<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\RebateOrder;
use App\Models\Report;
use App\Baqi\Alimns\AliMnsMsg;

include (base_path('extend/Alimsg/Alimsg.php')); 

class ProfitShareDayLog extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'profit:daylog';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '利润分发每日待审核记录日志';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {

        parent::__construct();
        //获取初始化开始结束时间
        $this->yesterday_start = date("Y-m-d",strtotime("-1 day"))." 00:00:00";
        $this->yesterday_end   = date("Y-m-d",strtotime("-1 day"))." 23:59:59";

    }

    /**
     * 核心业务流程
     *
     * @return mixed
     */
    public function handle()
    {
        $is_cz = Report::where('created_at', '>=', $this->yesterday_start)->where('created_at', '<=', $this->yesterday_end)->count();

        // $smss = new AliMnsMsg;
        
        $smss = new \ Alimsg();
        
        if($is_cz > 0){

            $result = $smss->send_sms('18595602136', 'reg', '重复添加已经结束流程');
            // $result = $smss->send_sms('13331128315', '重复添加已经结束流程', 'reg');
            die;//已经添加结束脚本流程
        }
        //实例话分润报表审核记录
        $info = new Report;

        $rebate = RebateOrder::where('created_at', '>=', $this->yesterday_start)->where('created_at', '<=', $this->yesterday_end)->where('is_return', 0);

        $info->order_num     = $rebate->count();
        $info->money_all     = $rebate->sum('total_price');
        $info->money         = $rebate->sum('rebate_amount');
        $info->money_company = $rebate->sum('platform_money');
        $info->money_shop    = $rebate->sum('shop_money');
        $info->money_queue   = $rebate->sum('debt_money');
        $info->money_product = $rebate->sum('rebate_money');
        $info->audit_date    = strtotime("-1 day");

        $res = $info->save();

        //发送短信通知
        if($res){
            $smss->send_sms('18595602136', 'reg', '分润审核记录添加成功');
            // $smss->send_sms('13331128315', '分润审核记录添加成功', 'reg');
        }else{
            $smss->send_sms('18595602136', 'reg', '分润审核记录添加失败');
            // $smss->send_sms('13331128315', '分润审核记录添加失败', 'reg');
        }

    }
}
